package cxt.cn.community.controller;


import cxt.cn.community.entity.Dto.*;
import cxt.cn.community.entity.User;
import cxt.cn.community.service.adminService;
import cxt.cn.community.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/*
 * @Author chenxiaotian
 * @Description:
 * @Date:2022122022/12/30
 * @Time:13:46
 */
@RestController
@RequestMapping("/api")
@Api(tags="系统 用户接口")
public class loginController {

    @Autowired
    adminService adminService;

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登陆")
    public CommonResult login(@RequestBody @Validated AdminLoginDto cmsLogin){
       User user = adminService.login(cmsLogin);
        if (user==null) {
            return CommonResult.failed("登陆失败，或存在用户名密码不正确");
        }
        return CommonResult.success(user);
    }
    /**
     * 用户注册
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public CommonResult register(@RequestBody @Validated AdminRegisterDto adminRegisterDto){
        int count = adminService.register(adminRegisterDto);
        if (count==0) {
            return CommonResult.failed("注册失败");
        }
        return CommonResult.success("注册成功");
    }

    /**
     * 用户登出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public CommonResult logout(@RequestBody @Validated LogoutDto logoutDto){
        adminService.logout(logoutDto.getUsername());
        return CommonResult.success("退出成功");
    }
    /**
     * 忘记密码
     * @return
     */
    @PostMapping("/modify")
    @ApiOperation("忘记密码")
    public CommonResult modify(@RequestBody @Validated ModifyDto modifyDto){
        int count = adminService.modify(modifyDto);
        if (count==0) {
            return CommonResult.failed("密码修改失败");
        }
        return CommonResult.success("密码修改成功");
    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("/modifyPassword")
    @ApiOperation("修改密码")
    public CommonResult modifyPassword(@RequestBody @Validated ModifyPassword modifyPassword){
        int count = adminService.modifyPassword(modifyPassword);
        if (count==0) {
            return CommonResult.failed("密码修改失败");
        }
        return CommonResult.success("密码修改成功");
    }

}
